<template>
  <div class="register-page">
    <h1>找回密码</h1>
    <AuthForm
        button-text="找回"
        :show-confirm="true"
        @submit="handleResetPassword"
    />
    <p v-if="error" class="error">{{ error }}</p>
    <p>已有账号？<router-link to="/login">立即登录</router-link></p>
  </div>
</template>

<script>
import AuthForm from '@/components/AuthForm.vue'

export default {
  name: 'ResetPasswordPage',
  components: { AuthForm },
  data() {
    return {
      error: ''
    }
  },
  methods: {
    async handleResetPassword(formData) {
      this.error = ''

      // 前端验证
      if (formData.password !== formData.password2) {
        this.error = '两次密码输入不一致'
        return
      }
      if (!formData.userName || !formData.password || !formData.password2) {
        this.error = '请填写完整的用户信息'
        return
      }
      if (formData.userName.length < 4) {
        this.error = '用户名至少包含4个字符'
        return
      }
      if (formData.password.length < 6) {
        this.error = '密码长度不能小于6位'
        return
      }

      try {
        const response = await this.$store.dispatch('auth/resetPassword', {
          userName: formData.userName,
          password1: formData.password,
          password2: formData.password2
        })

        console.log('完整注册响应:', response) // 调试用，正式环境可移除

        // 增强的成功判断逻辑
        const successConditions = [
          response?.code === 200,
          response?.Msg === '重置成功',
          response?.msg === '重置成功',
          response?.data?.code === 200,
          response?.data?.Msg === '重置成功',
          response?.data?.msg === '重置成功',
          response?.status === 'success'
        ]

        if (successConditions.some(condition => condition)) {
          this.$message.success('重置成功，正在跳转到登录页面...')
          setTimeout(() => {
            this.$router.push('/login')
          }, 1500)
          return
        }

        // 如果不符合任何成功条件，显示错误
        this.error = response?.Msg ||
            response?.msg ||
            response?.data?.msg ||
            response?.message ||
            '重置失败，请稍后再试'
      } catch (err) {
        console.error('重置错误详情:', err)

        // 更全面的错误处理
        if (err.code === 'ECONNABORTED') {
          this.error = '请求超时，请检查网络连接'
        } else if (err.response) {
          this.error = err.response.data?.msg ||
              err.response.data?.message ||
              err.response.statusText ||
              `服务器错误 (${err.response.status})`
        } else if (err.request) {
          this.error = '无法连接到服务器，请检查网络'
        } else {
          this.error = err.message || '重置过程中发生未知错误'
        }
      }
    }
  }
}
</script>

<style scoped>
.register-page {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  text-align: center;
}

.error {
  color: red;
  margin: 10px 0;
  font-size: 14px;
}

.register-page h1 {
  margin-bottom: 24px;
  color: #333;
}

.register-page p {
  margin-top: 16px;
  color: #666;
}

.register-page a {
  color: #1890ff;
  text-decoration: none;
}

.register-page a:hover {
  text-decoration: underline;
}
</style>